package com.ruoyi.web.controller.common.controller;

import com.aliyuncs.dysmsapi.model.v20170525.QuerySmsTemplateListResponse;
import com.ruoyi.common.constant.AliYunSmsConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.entity.BO.SmsSendBO;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.AliYunSmsUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.web.controller.member.mapper.IBackMemberMapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/back/AliYunSms")
public class BackAliYunSmsController extends BaseController {

    @Resource
    private AliYunSmsUtils aliYunSmsUtils;

    @Resource
    private IBackMemberMapper backMemberMapper;


    @PostMapping("/searchTemplateList")
    @ResponseBody
    public TableDataInfo searchTemplateList(BaseEntity baseEntity) {
        List<QuerySmsTemplateListResponse.SmsStatsResultDTO> list = new ArrayList<>();
        TableDataInfo result = new TableDataInfo();
        result.setMsg("查询成功！");
        try {
            QuerySmsTemplateListResponse response = aliYunSmsUtils.querySmsTemplateList(Convert.toInt(ServletUtils.getParameter(TableSupport.PAGE_NUM)), Convert.toInt(ServletUtils.getParameter(TableSupport.PAGE_SIZE)));
            List<QuerySmsTemplateListResponse.SmsStatsResultDTO> tmpList = response.getSmsTemplateList();
            tmpList.forEach(x -> {
                if (x.getAuditStatus().equals("AUDIT_STATE_PASS") && 1 == x.getTemplateType()) {
                    list.add(x);
                }
            });
            result.setRows(list);
            result.setTotal(list.size());
        } catch (BusinessException e) {
            //
            result.setTotal(0);
            result.setRows(list);
        }
        return result;
    }

    @PostMapping("/send")
    @ResponseBody
    public AjaxResult send(SmsSendBO smsSendBO) {
        try {
            List<String> phoneList = backMemberMapper.searchPhoneList(smsSendBO.getMemberIdStr());
            if (!phoneList.isEmpty()) {
                HashMap<String, String> paramMap = new HashMap<>();
                paramMap.put("code", smsSendBO.getTemplateCode());
                paramMap.put("signName", AliYunSmsConstants.SignEnum.match(ShiroUtils.getShopId()));
                phoneList.forEach(x -> {
                    paramMap.put("phone", x);
                    aliYunSmsUtils.send(paramMap);
                });
                return AjaxResult.success("发送成功");
            } else {
                return AjaxResult.error("请选择已手机授权的会员！");
            }
        } catch (BusinessException e) {

        }
        return AjaxResult.error("发送失败");
    }
}
